# -*- coding: utf-8 -*-
# =============================================================================
#     FileName:
#         Desc:
#       Author:
#        Email:
#     HomePage:
#      Version:
#   LastChange:
#      History:
# =============================================================================
import os
import sys
import json
import redis
import heapq
import argparse
import logging
import logging.handlers
from collections import namedtuple
from utils.logger_helper import LoggerHelper
from utils.common_config import CommonConfig
from utils.big_key_scanner import BigKeyScanner, RedisConfig

logger = logging.getLogger(name=CommonConfig.app_name)


class CommandParser(object):

    @classmethod
    def get_parser(cls):
        parser = argparse.ArgumentParser(
            description='从Redis扫描BIG KEY',
            add_help=False)
        connect_setting = parser.add_argument_group('参数信息')
        parser.add_argument(
            '--help',
            dest='help',
            action='store_true',
            help='获取帮助信息',
            default=False
        )
        connect_setting.add_argument(
            '--redis_host',
            dest='redis_host',
            type=str,
            default="",
            help='Redis服务器地址,默认值为127.0.0.1'
        )
        connect_setting.add_argument(
            '--redis_port',
            dest='redis_port',
            type=int,
            default=6379,
            help='Redis服务器端口,默认值为6379'
        )
        connect_setting.add_argument(
            '--redis_db',
            dest='redis_db',
            type=int,
            default=0,
            help="Redis服务器DB,默认值为0"
        )
        connect_setting.add_argument(
            '--redis_password',
            dest='redis_password',
            type=str,
            default="",
            help="Redis密码,默认值为空字符串"
        )
        connect_setting.add_argument(
            '--min_key_size',
            dest='min_key_size',
            type=int,
            default=1000,
            help="最小key大小(单位Byte),默认值1000"
        )
        connect_setting.add_argument(
            '--top_key_size',
            dest='top_key_size',
            type=int,
            default=1000,
            help="返回大Key的数量,默认值1000"
        )
        connect_setting.add_argument(
            '--top_key_type',
            dest='top_key_type',
            type=str,
            default="all",
            help="要扫描的大KEY类型，默认值为ALL"
        )
        connect_setting.add_argument(
            '--scan_key_count',
            dest='scan_key_count',
            type=int,
            default=0,
            help="扫描KEY的数量，默认值0(扫描全部),当扫描KEY数量大于0时，仅扫描指定数量的key便停止扫描"
        )
        return parser

    @classmethod
    def parse_args(cls, command_args):
        need_print_help = False if command_args else True
        parser = cls.get_parser()
        args = parser.parse_args(command_args)
        if args.help or need_print_help:
            parser.print_help()
            sys.exit(1)
        return args


def main(command_args):
    logger.info("开始扫描...")
    args = CommandParser.parse_args(command_args)
    bks = BigKeyScanner(
        redis_config=RedisConfig(
            redis_host=args.redis_host,
            redis_port=args.redis_port,
            redis_password=args.redis_password,
            redis_db=args.redis_db
        ),
        min_key_size=args.min_key_size,
        top_key_size=args.top_key_size,
        top_key_type="all",
        scan_key_count=args.scan_key_count
    )
    bks.scan_redis_keys(scan_count=100)
    logger.info("完成扫描...")


if __name__ == '__main__':
    LoggerHelper.init_logger(
        logger_name=CommonConfig.app_name,
        logger_level=logging.INFO,
        logger_dir=CommonConfig.app_log_dir
    )
    main(sys.argv[1:])
